// 创建实用工具类
// 1. 获取系统时间
// 2. 获取文件大小
// 3. 创建目录
// 4. 获取文件所在目录
#ifndef __M_UTIL_H__
#define __M_UTIL_H__

#include <iostream>
#include <ctime>
#include <sys/stat.h>

namespace mylog {
    namespace util {
        class Date {
            public:
                static size_t now() {
                    return (size_t)time(nullptr);
                }
        };
        class File {
            public:
                static bool exists(const std::string &pathname) {
                    struct stat st;
                    return stat(pathname.c_str(), &st) == 0;
                    
                }
                static std::string path(const std::string &pathname) {
                    // ./abc/bca/cba
                    if (pathname.empty()) return ".";
                    size_t pos = pathname.find_last_of("/\\");
                    if(pos == std::string::npos) return ".";
                    return pathname.substr(0, pos+1);
                }
                static void creatDirectory(const std::string &pathname) {
                    if (pathname.empty()) return ;
                    //if (exists(pathname)) return ;
                    size_t pos, idx = 0;
                    while (idx < pathname.size())
                    {
                        
                        pos = pathname.find_first_of("/\\", idx);
                        if (pos == std::string::npos) {
                            mkdir(pathname.c_str(), 0777);
                            return;
                        }
                        if (pos == idx) {idx = pos+1; continue;}
                        std::string subdir = pathname.substr(0, pos);
                        if (subdir == "." || subdir == "..") {idx = pos+1; continue;}
                        if (exists(subdir)) {idx = pos+1; continue;}
                        mkdir(subdir.c_str(), 0777);
                        idx = pos+1;
                    }
                    
                    
                }
        };
    }
}

#endif